package com.shuidihuzhu.sdb.service;

import com.shuidihuzhu.sdb.entity.User;
import com.shuidihuzhu.sdb.helper.log.annotation.LogMethodInvoke;
import com.shuidihuzhu.sdb.helper.log.annotation.LogTrace;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Component
public class SomeService {

    @LogTrace(key = "#p.name + ' ' + #p.age", mdcKey = "CCC")
    @LogMethodInvoke(level = "DEBUG", keyWords = "转换为列表")
    public List<User> toList(User user) {
        log.info("toList {}", user);
        return List.of(user);
    }

    @LogTrace(key = "#p[0].name + ' ' + #p[0].age + '::' + #p[1]")
    @LogMethodInvoke(level = "ERROR", keyWords = "转换为列表")
    public List<User> toList2(User user, Integer cnt) {
        if (cnt == null) {
            return toList(user);
        }
        List<User> list = new ArrayList<>();
        for (Integer i = 0; i < cnt; i++) {
            list.add(user);
        }
        return list;
    }

    @LogMethodInvoke(level = "ERROR", keyWords = "转换为列表")
    public User error(int age) {
        Assert.isTrue(age > 0, "年龄超限");
        return null;
    }
}
